#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int n, a, b, c, d;
    cin >> n;
    if (n >= 1000 && n <= 9999) {
        a = n / 1000;
        b = (n / 100) % 10;
        c = (n / 10) % 10;
        d = n % 10;
        int max_prime = 0;
        bool prime_n = true;
        for (int i = 2; i <= sqrt(n); i++) {
            if (n % i == 0) {
                prime_n = false;
                break;
            }
        }
        if (prime_n && n > max_prime) {
            max_prime = n;
        }
        bool prime_abc = true;
        if (a * 100 + b * 10 + c <= n) {
            for (int i = 2; i <= sqrt(a * 100 + b * 10 + c); i++) {
                if ((a * 100 + b * 10 + c) % i == 0) {
                    prime_abc = false;
                    break;
                }
            }
            if (prime_abc && a * 100 + b * 10 + c > max_prime) {
                max_prime = a * 100 + b * 10 + c;
            }
        }
        bool prime_bcd = true;
        if (b * 100 + c * 10 + d <= n) {
            for (int i = 2; i <= sqrt(b * 100 + c * 10 + d); i++) {
                if ((b * 100 + c * 10 + d) % i == 0) {
                    prime_bcd = false;
                    break;
                }
            }
            if (prime_bcd && b * 100 + c * 10 + d > max_prime) {
            max_prime = b * 100 + c * 10 + d;
            }
        }
        cout << max_prime;
    } else {
        cout << "0";
    }
    return 0;
}
